package com.kwum.file;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;


public class FileUtils {

	private final static String filePath = "F:\\CloudMusic\\MV\\胡彦斌,Jony J - 返老还童.mp4";
	
	private final static String CODE_MD5 = "MD5";
	private final static String CODE_SHA1 = "SHA1";
	
	public static void main(String[] args) throws Exception {
		File file = new File(filePath);
		System.out.println("size：" + file.length());
		getCoding(file, CODE_MD5);
		getCoding(file, CODE_SHA1);
	}
	
	/**
	 * 计算文件的编码
	 * @param file 文件
	 * @param code MD5, SHA1, SHA-256, SHA-384, SHA-512
	 * @throws Exception
	 */
	public static void getCoding(File file, String code) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        MessageDigest md = MessageDigest.getInstance(code);
        byte[] buffer = new byte[1024];
        int length = -1;
        while ((length = fis.read(buffer, 0, 1024)) != -1) {
            md.update(buffer, 0, length);
        }
        BigInteger bigInt = new BigInteger(1, md.digest());
        System.out.println(code + "：" + bigInt.toString(16));
        fis.close();
    }
}
